<template>
  <div class="main-content">
    <h3>讲师管理</h3>

    <query-line @getTeacherList="this.queryTeacher"/>

    <el-table
      v-loading="loading"
      :data="tableData"
      border
      fit
      highlight-current-row>
      <el-table-column
        label="序号"
        width="80"
        align="center">
        <template slot-scope="scope">
          {{ (page - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column
        prop="name"
        label="讲师姓名"
        width="140">
      </el-table-column>
      <el-table-column
        width="100"
        label="讲师头衔">
        <template slot-scope="scope">
          {{scope.row.level === 0 ? '高级讲师' : '首席讲师'}}
        </template>
      </el-table-column>
      <el-table-column prop="dept" label="所处部门"/>
      <el-table-column prop="introduce" label="资历"/>
      <el-table-column prop="gmtCreate" label="添加时间" width="160"/>
      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="scope">
          <router-link :to="'/teacher/edit/'+scope.row.id">
            <el-button type="primary" size="mini" icon="el-icon-edit">修改</el-button>
          </router-link>
          <el-button type="danger" size="mini" icon="el-icon-delete" @click="removeDataById(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination :page="this.page" :limit="this.limit" :total="this.total" @changePage="pageChange"/>
  </div>
</template>

<script>
  import teacher from "../../../../api/teacher";
  import Pagination from "../submodules/Pagination";
  import QueryLine from "../submodules/QueryLine";
  export default {
    name: "TeacherList",
    mounted() {
      this.getList()
    },
    components:{
      QueryLine,
      Pagination,
    },
    data() {
      return {
        values: '',
        loading: false,
        tableData: null,//查询之后接口返回集合
        page:1,//当前页
        limit:10,//每页记录数
        total:0,//总记录数
        teacherQuery:{}, //条件封装对象
      }
    },
    methods: {
      getList(page = 1) {
        this.loading = true
        this.page = page
        teacher.getTeacherQuery(this.page, this.limit, this.teacherQuery)
        .then(resp => {
          // console.log(resp)
          this.tableData = resp.data.rows
          this.total = resp.data.total
          this.loading = false
        }).catch(err => {
          setTimeout(() => {
            this.$message.error('请稍后重试')
            this.loading = false
          },3000)
        })
      },
      //query
      queryTeacher(queryList){
        this.teacherQuery = queryList
        this.getList()
      },
      //删除讲师
      removeDataById(id) {
        this.$confirm('此操作将永久删除讲师记录, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {  //点击确定，执行then方法
          //调用删除的方法
          teacher.deleteTeacherId(id)
            .then(response => {//删除成功
              //提示信息
              this.$message({
                type: 'success',
                message: '删除成功!'
              });
              //回到列表页面
              this.getList()
            }).catch(err => {
              this.$message.error('删除失败')
          })
        }) //点击取消，执行catch方法
      },
      pageChange(currentPage){
        this.page = currentPage
        // console.log(currentPage)
        this.getList(this.page)
      }
    }
  }
</script>

<style scoped>
  .main-content{
    padding: 0 36px;
  }
</style>
